package com.dkd.service.impl;

import cn.elegent.ac.ElegentAC;
import cn.elegent.actx.annotation.ACTransaction;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.config.ACTXConfig;
import com.dkd.config.TopicConfig;
import com.dkd.contract.AutoSupplyContract;
import com.dkd.contract.VendoutResultContract;
import com.dkd.dao.VendoutRunningDao;
import com.dkd.entity.VendoutRunningEntity;
import com.dkd.service.VendoutRunningService;
import com.dkd.vo.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class VendoutRunningServiceImpl extends ServiceImpl<VendoutRunningDao, VendoutRunningEntity> implements VendoutRunningService {

    @Autowired
    private ElegentAC elegentAC;

    /**
     * 处理出货结果通知的方法
     *
     * @param vendoutResultContract
     */
    @Override
    @ACTransaction(mainName = ACTXConfig.ORDER_VENDOUT,
            childName = ACTXConfig.ORDER_VENDOUT_RESULT)
    public void vendOutResult(VendoutResultContract vendoutResultContract) {
        if (vendoutResultContract.isSuccess()) {
            try {
                //查询出货记录
                var runningEntity = this.getOne(Wrappers.<VendoutRunningEntity>lambdaQuery()
                        .eq(VendoutRunningEntity::getOrderNo
                                , vendoutResultContract.getVendoutData().getOrderNo()));
                runningEntity.setStatus(true);
                this.updateById(runningEntity);
                log.info("售货机微服务处理");


                //发送自动补货工单检查
                AutoSupplyContract autoSupplyContract = new AutoSupplyContract();
                autoSupplyContract.setInnerCode(vendoutResultContract.getInnerCode());
                autoSupplyContract.setSkuId(vendoutResultContract.getVendoutData().getSkuId());
                this.elegentAC.publish(TopicConfig.VMS_AUTO_SUPPLY_TOPIC, autoSupplyContract);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("出货结果日志记录报错，data ： " + vendoutResultContract);
            }
        } else {
            log.info("出货失败处理");
            throw new RuntimeException("出货失败：" + vendoutResultContract);
        }
    }

    @Override
    public Pager<VendoutRunningEntity> findPage(long pageIndex, long pageSize, Map searchMap) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<VendoutRunningEntity> page =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageIndex, pageSize);

        QueryWrapper queryWrapper = createQueryWrapper(searchMap);
        this.page(page, queryWrapper);

        Pager<VendoutRunningEntity> pageResult = new Pager<>();
        pageResult.setCurrentPageRecords(page.getRecords());
        pageResult.setPageIndex(page.getCurrent());
        pageResult.setPageSize(page.getSize());
        pageResult.setTotalCount(page.getTotal());
        return pageResult;
    }


    @Override
    public List<VendoutRunningEntity> findList(Map searchMap) {
        QueryWrapper queryWrapper = createQueryWrapper(searchMap);
        return this.list(queryWrapper);
    }

    /**
     * 条件构建
     *
     * @param searchMap
     * @return
     */
    private QueryWrapper createQueryWrapper(Map searchMap) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (searchMap != null) {
            queryWrapper.allEq(searchMap);
        }
        return queryWrapper;
    }

}
